package com.gfxy.educational.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * @author: lijixin 2019-05-23
 */
@Configuration
public class QuartzConfiguration {



    @Bean(name = "scheduler")
    public Scheduler getScheduler() throws SchedulerException {
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
        Scheduler scheduler = schedFact.getScheduler();
//        scheduler.start();

        JobDetail job = newJob(JobTimeShiftTask.class)
                .withIdentity("timeShift","timeShift")
                .storeDurably()
                .build();
        Trigger summerTrigger = newTrigger()
                .withIdentity(JobTimeShiftTask.SUMMER_TIME,"timeShift")
                .withSchedule(cronSchedule("0 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * ? "))
                .forJob(job)
                .build();
        Trigger winterTrigger = newTrigger()
                .withIdentity(JobTimeShiftTask.WINTER_TIME,"timeShift")
                .withSchedule(cronSchedule("0 1,3,5,7,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * ? "))
                .forJob(job)
                .build();
        scheduler.addJob(job, true);
        scheduler.scheduleJob(summerTrigger);
        scheduler.scheduleJob(winterTrigger);


        return scheduler;
    }

}
